﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;

namespace WindowsFormsApplication2.CLASS
{
   public class Worker
    {
        
        #region " & PropertyS & "
        private string _WorkerId;
        public string WorkerId
        {
            get { return _WorkerId; }
            set { _WorkerId = value; }
        }
        private string _WorkerFName;
        public string WorkerFName
        {
            get { return _WorkerFName; }
            set { _WorkerFName = value; }
        }
        private string _WorkerLName;
        public string WorkerLName
        {
            get { return _WorkerLName; }
            set { _WorkerLName = value; }
        }
        private string _WorkerRd;
        public string WorkerRd
        {
            get { return _WorkerRd; }
            set { _WorkerRd = value; }
        }
        private string _WorkerAddress;
        public string WorkerAddress
        {
            get { return _WorkerAddress; }
            set { _WorkerAddress = value; }
        }
        private string _WorkerPhone;
        public string WorkerPhone
        {
            get { return _WorkerPhone; }
            set { _WorkerPhone = value; }
        }
        private string _WorkerType;
        public string WorkerType
        {
            get { return _WorkerType; }
            set { _WorkerType = value; }
        }
        private string _WorkerPass;
        public string WorkerPass
        {
            get { return _WorkerPass; }
            set { _WorkerPass = value; }
        }
        #endregion
        public Worker()
        {
            //butsets

        }
        public Worker(string _WorkerId, string _WorkerFName, string _WorkerLName, string _WorkerRd, string _WorkerAddress, string _WorkerPhone, string _WorkerType, string _WorkerPass)
        {
            this.WorkerId = _WorkerId;
            this.WorkerFName = _WorkerFName;
            this.WorkerLName = _WorkerLName;
            this.WorkerRd = _WorkerRd;
            this.WorkerAddress = _WorkerAddress;
            this.WorkerPhone = _WorkerPhone;
            this.WorkerType = _WorkerType;
            this.WorkerPass = _WorkerPass;


        }
        public static Worker getbyRec(string _byWorkerId)
        {
            Worker x = new Worker();

            string qry1 = string.Format("select *from  Worker where WorkerId=('{0}')", _byWorkerId);
            DataTable table = DBTools.selecQryTmp(qry1);

            try
            {
                return new Worker(table.Rows[0]["WorkerId"].ToString(),table.Rows[0]["WorkerFirstName"].ToString(),table.Rows[0]["WorkerLasName"].ToString(),table.Rows[0]["WorkerRd"].ToString(),table.Rows[0]["WorkerAddress"].ToString(),table.Rows[0]["WorkerPhone"].ToString(),table.Rows[0]["WorkerType"].ToString(),table.Rows[0]["WorkerPass"].ToString());
            }
            catch (Exception e)
            {
                //MsgBox(ex.Message, MsgBoxStyle.OkCancel, "account->getAccountByRecID")'
                MessageBox.Show(string.Format("{0} Exception caught.", e));

                return null;
            }



        }
        public static bool AddFunction(string _WorkerId, string _WorkerFName, string _WorkerLName, string _WorkerRd, string _WorkerAddress, string _WorkerPhone, string _WorkerType, string _WorkerPass)
        {

            //DBTools._Conn.Open();
            DBTools._Comm.CommandText = "StoredProcedureWorker";
            DBTools._Comm.CommandType = CommandType.StoredProcedure;
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerId", SqlDbType.NVarChar, 8)).Value = _WorkerId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerFName", SqlDbType.NVarChar, 25)).Value = _WorkerFName;
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerLName", SqlDbType.NVarChar, 25)).Value = _WorkerLName;
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerRd", SqlDbType.NVarChar, 10)).Value = _WorkerRd;
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerAddress", SqlDbType.NVarChar, 50)).Value = _WorkerAddress;
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerPhone", SqlDbType.NVarChar, 8)).Value = _WorkerPhone;
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerType", SqlDbType.NVarChar, 10)).Value = _WorkerType;
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerPass", SqlDbType.NVarChar, 16)).Value = _WorkerPass;


            DBTools._Comm.Parameters.Add(new SqlParameter("@Otype", SqlDbType.NVarChar, 4)).Value = "ADD";
            SqlParameter prm = new SqlParameter("@returnValue", SqlDbType.TinyInt);
            DBTools._Comm.Parameters.Add(prm);
            prm.Direction = ParameterDirection.Output;
            string isOk;
            try
            {
                int rowcount = DBTools._Comm.ExecuteNonQuery();
                DBTools._Comm.Parameters.Clear();
                isOk = Convert.ToString(prm.Value);



                if (isOk == "1")
                {
                    return true;

                }
                else
                {
                    return false;

                }
            }


            catch (SqlException ex)
            {
                DBTools._Comm.Parameters.Clear();
                MessageBox.Show(ex.Message);


            }

            return false;


        }
        public static bool EditFunction(string _byWorkerId, string _byWorkerFName, string _byWorkerLName, string _byWorkerRd, string _byWorkerAddress, string _byWorkerPhone, string _byWorkerType, string _byWorkerPass)
        {


           // DBTools._Conn.Open();
            DBTools._Comm.CommandText = "StoredProcedureWorker";
            DBTools._Comm.CommandType = CommandType.StoredProcedure;
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerId", SqlDbType.NVarChar, 8)).Value = _byWorkerId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerFName", SqlDbType.NVarChar, 25)).Value = _byWorkerFName;
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerLName", SqlDbType.NVarChar, 25)).Value = _byWorkerLName;
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerRd", SqlDbType.NVarChar, 10)).Value = _byWorkerRd;
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerAddress", SqlDbType.NVarChar, 50)).Value = _byWorkerAddress;
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerPhone", SqlDbType.NVarChar, 8)).Value = _byWorkerPhone;
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerType", SqlDbType.NVarChar, 10)).Value = _byWorkerType;
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerPass", SqlDbType.NVarChar, 8)).Value = _byWorkerPass;

            DBTools._Comm.Parameters.Add(new SqlParameter("@Otype", SqlDbType.NVarChar, 4)).Value = "EDIT";
            SqlParameter prm = new SqlParameter("@returnValue", SqlDbType.TinyInt);
            DBTools._Comm.Parameters.Add(prm);
            prm.Direction = ParameterDirection.Output;
            string isOk;
            try
            {
                int rowcount = DBTools._Comm.ExecuteNonQuery();
                DBTools._Comm.Parameters.Clear();
                isOk = Convert.ToString(prm.Value);



                if (isOk == "1")
                {
                    return true;

                }
                else
                {
                    return false;

                }
            }


            catch (SqlException ex)
            {
                DBTools._Comm.Parameters.Clear();
                MessageBox.Show(ex.Message);


            }

            return false;



        }

        public static bool DelFunction(string _byWorkerId)
        {


            DBTools._Comm.CommandText = "StoredProcedureWorker";
            DBTools._Comm.CommandType = CommandType.StoredProcedure;
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerId", SqlDbType.NVarChar, 8)).Value = _byWorkerId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerFName", SqlDbType.NVarChar, 25)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerLName", SqlDbType.NVarChar, 25)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerRd", SqlDbType.NVarChar, 10)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerAddress", SqlDbType.NVarChar, 50)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerPhone", SqlDbType.NVarChar, 8)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerType", SqlDbType.NVarChar, 10)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@WorkerPass", SqlDbType.NVarChar, 16)).Value = "";


            DBTools._Comm.Parameters.Add(new SqlParameter("@Otype", SqlDbType.NVarChar, 4)).Value = "DEL";
            SqlParameter prm = new SqlParameter("@returnValue", SqlDbType.TinyInt);
            DBTools._Comm.Parameters.Add(prm);
            prm.Direction = ParameterDirection.Output;
            string isOk;
            try
            {
                int rowcount = DBTools._Comm.ExecuteNonQuery();
                DBTools._Comm.Parameters.Clear();
                isOk = Convert.ToString(prm.Value);



                if (isOk == "1")
                {
                    return true;

                }
                else
                {
                    return false;

                }
            }


            catch (SqlException ex)
            {
                DBTools._Comm.Parameters.Clear();
                MessageBox.Show(ex.Message);


            }

            return false;



        }
    }
}

